<template>
  <div class="login-container">
    <div class="login-form">
      <h2>用户登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="userName">邮箱或手机号</label>
          <input type="text" id="userName" v-model="userName" required>
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" id="password" v-model="password" required>
        </div>
        <button type="submit" class="login-button">登录</button>
        <p v-if="error" class="error-message">{{ error }}</p>
      </form>
      <div class="extra-links">
        <span>还没有账户？</span>
        <router-link to="/register">立即注册</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex';

export default {
  name: 'LoginPage',
  data() {
    return {
      userName: '',
      password: '',
      error: ''
    };
  },
  methods: {
    ...mapActions('user', ['login']),
    async handleLogin() {
      try {
        this.error = '';
        await this.login({
          userName: this.userName,
          password: this.password
        });
        this.$router.push('/');
      } catch (err) {
        this.error = err.message || '登录失败，请稍后再试。';
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  background: #e6f9f0;
}
.login-form {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  background-color: #f6fffa;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(66, 185, 131, 0.08);
  border: 1px solid #b2e5d6;
}
h2 {
  text-align: center;
  margin-bottom: 25px;
  color: #2ca06c;
}
.form-group {
  margin-bottom: 20px;
}
.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #42b983;
}
.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #b2e5d6;
  border-radius: 4px;
  background: #f6fffa;
  box-sizing: border-box;
}
.login-button {
  width: 100%;
  padding: 12px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.2s;
}
.login-button:hover {
  background-color: #2ca06c;
}
.error-message {
  color: #e53935;
  text-align: center;
  margin-top: 15px;
}
.extra-links {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
}
.extra-links a {
  color: #42b983;
  text-decoration: none;
}
</style> 